﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.UI;
using WinnerCMS.BLL;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.WebFactory
{
    public class PhotoShow : PublicPage<Photo>
    {
        public PhotoShow()
        {

        }
        
        #region 重写Render
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder winner = new StringBuilder();
            using (TextWriter tempWriter = new StringWriter())
            {
                base.Render(new HtmlTextWriter(tempWriter));
                winner.Append(tempWriter.ToString());
            }
            winner.Replace("[CategoryId]", this.Model.CategoryId.ToString());
            winner.Replace("[ContentId]", this.Model.Id.ToString());

            IList<string> matchresult = TagEngine.TagCollection(winner.ToString(), "PhotoList");
            foreach (string nextmatch in matchresult)
            {
                winner.Replace(nextmatch, PhotoCommon.PhotoList(nextmatch, this.Model.ContentPhotoLists));
            }

            matchresult = TagEngine.TagCollection(winner.ToString(), "CommentShow");
            foreach (string nextmatch in matchresult)
            {
                if (this.Model.AllowComment )
                {
                    winner.Replace(nextmatch, CommentCommon.CommentShow(nextmatch, this.Model.Id));
                }
                else
                {
                    winner.Replace(nextmatch, string.Empty);
                }
            }

            CategoryCommon.Path(winner, this.Model.CategoryId);


            PageCommon.RepalceFiled(winner, this.Model);

            if (Request["Method"] != null)
            {
                string path = StaticFilePath.ContentShowUrl(this.Model);
                new FileIO().WriterFile(HttpServer.MapPath(path), winner.ToString());

                writer.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff"));
            }
            else
            {
                writer.Write(winner.ToString());
            }
        }
        #endregion
    }
}